var member_id = '',
	ws,
	spage = '';
document.addEventListener('plusready',function(){
	member_id = plus.storage.getItem('id');
	ws = plus.webview.currentWebview();
	spage = ws.spage;
//	console.log(spage);
	$.ajax({ 
		type:"post", 
		url:apiRoot+"?act=member_address&op=address_list",
		data:{member_id:member_id},
		dataType:'json',
		success:function(data){ 
			console.log(JSON.stringify(data));
			if(data.length > 0){
				var html = '';
				$.each(data, function(k,v) {
					html += '<div class="my_addr-list del'+ k +'">'+
								'<ul class="mui-table-view">'+
									'<li class="mui-table-view-cell">'+
										'<span>'+ v.true_name +'</span>'+
										'<span class="mui-pull-right">'+ v.mob_phone +'</span>'+
										'<p class="back m5">'+ v.area_info + ' ' + v.address +'</p>'+
									'</li>'+
								'</ul>'+
								'<ul class="mui-table-view">'+
									'<li class="mui-table-view-cell">'+
										'<div class="mui-radio mui-left">'+
											'<label>设为默认</label>'+
											'<input class="default" name="radio" value="Item 1" type="radio" '+ ((v.is_default == 1)?"checked":"") +' data-id="'+ v.address_id +'" data-ph="'+ v.mob_phone +'" data-ar="'+ v.area_info +'" data-add="'+ v.address +'" data-name="'+ v.true_name +'">'+
										'</div>'+
										'<div class="mui-pull-right compile">'+
											'<a class="upl" data-id="'+ v.address_id +'">'+
												'<i class="mui-icon iconfont" style="vertical-align: middle;">&#xe60f;</i>'+
												'<span class="font-14" style="vertical-align: top; margin-right:20px;">编辑</span>'+
											'</a>'+
									 		'<a class="del" data-id="'+ v.address_id +'" data-k="'+ k +'">'+
												'<i class="mui-icon iconfont">&#xe610;</i>'+
												'<span class="font-14" style="vertical-align: top">删除</span>'+
											'</a>'+
										'</div>'+
									'</li>'+
								'</ul>'+
							'</div>';
				});
				$('.mui-content').html(html);
			}
			//编辑 
			$('.upl').unbind('tap');
			$('.upl').on('tap',function(){
				var _this = $(this);
				var address_id = _this.attr('data-id');
				var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
				var webviewContent= plus.webview.create('./edit-addr.html','edit-addr.html',{},{address_id:address_id,wsid:ws.id});//后台创建webview并打开show.html
				webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
			        nwaiting.close(); //新webview的载入完毕后关闭等待框
			        webviewContent.show("slide-in-right",300); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
			    }, false);
			})
			//添加地址
			$('.new').on('click',function(){
				var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
				var webviewContent= plus.webview.create('./new-addr.html','new-addr.html',{},{wsid:ws.id});//后台创建webview并打开show.html
				webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
			        nwaiting.close(); //新webview的载入完毕后关闭等待框
			        webviewContent.show("slide-in-right",300); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
			    }, false);
			})
			//删除
			$('.del').on('click',function(){
				var _this = $(this);
				var address_id = _this.attr('data-id');
				var k = _this.attr('data-k');
				$.ajax({
					type:"post",
					url:apiRoot + "?act=member_address&op=address_del",
					data:{
						member_id:member_id,
						address_id:address_id
					},
					dataType:'json',
					success:function(data){
//						console.log(JSON.stringify(data));
						if(data.code == 200){
							toast('删除成功！');
							$('.del'+k).remove();
						}else{
							toast('删除失败！');
						}
					},
					error:function(e){
						toast('连接失败！');
					}
				});
			})
			//设置默认地址
			$("input:radio[name='radio']").on('click', function() {
					var address_id = $(this).attr('data-id');
//					console.log(address_id+'--'+tname+'--'+address+'--'+phone+'--'+areas);
					$.ajax({  
						type: "post",
						url: apiRoot + "?act=index&op=default",
						data:{
							address_id : address_id,
							member_id:member_id
						},
						dataType: 'json',
						success: function(data) {
							console.log(JSON.stringify(data));
							if(data.code == 200) {
								toast(data.datas);
								console.log(spage);
								if(spage){
									plus.webview.getWebviewById(spage).reload();
//									mui.fire(cur, 'address',{tname:data.addr.true_name,address:data.addr.address,areas:data.addr.area_info,phone:data.addr.mob_phone,addr_id:data.addr.address_id,city_id:data.addr.city_id});
//////									cur.evalJS('goods_inf()');
									setTimeout(function(){
										ws.close()
									},10); 
								}
							}else{
								toast(data.datas.error);								
							}
						},
						error: function(e) {
							alert("网络异常，请重试!");
						}, 
					});
				
				})
		}, 
		error:function(e){
			toast('连接失败！');
		}
	});
},false);